//
//  DingyueDetailsController.m
//  网易新闻
//
//  Created by dllo on 15/10/8.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "DingyueDetailsController.h"
#import "DingyueDetailsCell.h"
#import "DetailsController.h"
#import "DingyueDetailsModel.h"
@interface DingyueDetailsController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)DetailsController *result;
@end
@implementation DingyueDetailsController

- (void)viewWillLayoutSubviews{
    [self.table.header beginRefreshing];
}

- (void)createTableView{
    [self setTableView];
    self.table.delegate = self;
    self.table.dataSource = self;

    [self.table registerClass:[DingyueDetailsCell class] forCellReuseIdentifier:@"DingyueDetailsCell"];
    self.table.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handelDataWithURL:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", self.tid] WithType:0];
        [self.table.header endRefreshing];
    }];
    self.table.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handelDataWithURL:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/%lu-20.html", self.tid, self.modArr.count] WithType:1];
        [self.table.footer endRefreshing];
    }];
}

- (void)handelDataWithURL:(NSString *)url WithType:(NSInteger)type{
    [AFTool GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (type == 0) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:self.tid]];
        }else if (type == 1){
            [self.modArr addObjectsFromArray:[tempDic objectForKey:self.tid]];
        }
        [tempDic release];
        [self.table reloadData];
    } fail:^(NSError *error) {
        NSLog(@"404(DingyueDetailsController)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DingyueDetailsModel *mod = [DingyueDetailsModel modWithDic:self.modArr[indexPath.row]];
    DingyueDetailsCell *cell = [[DingyueDetailsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DingyueDetailsCell"];
    cell.mod = mod;
    self.cellheight = cell.height;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.result = [[DetailsController alloc]init];
    self.result.docid = [self.modArr[indexPath.row]objectForKey:@"docid"];;
    [self.navigationController pushViewController:self.result animated:YES];
    [self.result release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)dealloc{
    self.table.dataSource = nil;
    self.table.delegate = nil;
    [_tid release];
    [super dealloc];
}


@end
